package com.ruoyi.ex.domain;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 自测题库对象 ex_test_bank
 *
 * @author zzx
 * @date 2023-03-25
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@ToString
public class ExTestBank extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    private Long id;

    /**
     * 用户编号
     */
    @Excel(name = "用户编号")
    private Long userId;

    /**
     * 测试名称
     */
    @Excel(name = "测试名称")
    private String name;

    /**
     * 测试时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    @Excel(name = "测试时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date testDate;

    /**
     * 开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    @Excel(name = "开始时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date beginTime;

    /**
     * 结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    @Excel(name = "结束时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

    /**
     * 总题数
     */
    @Excel(name = "总题数")
    @NotNull(message = "total不可为空")
    private Integer total;

    /**
     * 对题数
     */
    @Excel(name = "对题数")
    private Long right;

    /**
     * 题目列表
     */
    private String questionIds;
    private JSONArray questionIdList;

    /**
     * 答案列表
     */
    private String answerList;
    private Map<Long,Object> answerMap;

    /**
     * 得分
     */
    @Excel(name = "得分")
    private Double score;

    /**
     * 状态
     */
    @Excel(name = "状态")
    private Integer status;

    /**
     * 预留字段1
     */
    private String reservedOne;

    /**
     * 预留字段2
     */
    @Excel(name = "预留字段2")
    private String reservedTwo;


    /**
     * 选题类型
     */
    @NotNull(message = "选择类型不可为空[sig: ratio按题库比例、average平均选择、optional自选]")
    private String sig;

    /**
     * 题库列表
     */
    @NotNull(message = "题库列表不可为空")
    private Map<Long,Integer> questionCheckeds;

    //格式化已完成的题目
    private Map<Long, Boolean> questionAnswers;

    public void setAnswerList(String answerList) {
        this.answerList = answerList;
        this.answerMap = new HashMap<>();
        try{
            JSONUtil.parseObj(answerList).forEach((k,v)->{
                answerMap.put(Long.valueOf(k),v);
            });
        }catch (Exception e){
            StaticLog.error(e);
        }

    }

    public void setQuestionIds(String questionIds) {
        this.questionIds = questionIds;
        this.questionIdList =new JSONArray(questionIds);
    }
}
